package com.carrie.config.handler.global;

import com.carrie.tools.LogUtils;
import com.carrie.tools.Result;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.ResponseStatusException;

import java.io.IOException;

/**
 * @author XuHaiLong
 * @since 2025/4/16 08:43
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(IOException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handleIOException(IOException e) {
        LogUtils.error("处理文件上传时发生 IO 异常: ", e);
        return new Result(1010, e.getMessage());
    }
    @ExceptionHandler(BadCredentialsException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result handleAuthenticationException(BadCredentialsException e) {
        return new Result(1001, e.getMessage());
    }
    @ExceptionHandler(UsernameNotFoundException.class)
    public Result handleUsernameNotFoundException(UsernameNotFoundException ex) {
        return new Result(1007,ex.getMessage());
    }
}
